﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using LdtJudge.DatabaseHandle;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 法定情节调节法定刑表
    /// </summary>
    public class XF_Punishiment_AdjustmentDAL
    {
        private string DbName;
        public XF_Punishiment_AdjustmentDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(XF_Punishiment_AdjustmentModel model)
        {
            serverhelper sqlserverhelper = new serverhelper("");
            string primaryKey = sqlserverhelper.CreateKeyID();
            string sql=string.Format(@"insert into XF_Punishiment_Adjustment(
			PA_ID,CC_ID,PA_SentenceID,PA_influenceTypeID,PA_AdjustTypeID)
			values ('{0}','{1}','{2}','{3}','{4}')", primaryKey,model.CC_ID,model.PA_SentenceID,model.PA_influenceTypeID,model.PA_AdjustTypeID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ccId)
        {
            string sql = string.Format(@" delete from XF_Punishiment_Adjustment where CC_ID='{0}' ", ccId);           
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取法定情节调节法定刑数据
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetAdjustment(string ccid)
        {
            string sql = string.Format(@" SELECT *  FROM [Ldt_JudgeDocSystem].[dbo].[XF_Punishiment_Adjustment] where CC_ID='{0}' ", ccid);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 修改字典值名称，通过Am_ID
        /// </summary>
        public bool UpdateName(XF_Punishiment_AdjustmentModel model)
        {
            string sql = string.Format(@" update XF_Punishiment_Adjustment set PA_SentenceID='{0}',PA_influenceTypeID='{1}',PA_AdjustTypeID='{2}' where CC_ID='{3}' ", model.PA_SentenceID, model.PA_influenceTypeID, model.PA_AdjustTypeID, model.CC_ID);            
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        

    }
}
